import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import '../utils/http.dart';
//登录注册公用组件
import './common.dart';
// 保存状态存储
import 'package:shared_preferences/shared_preferences.dart';

class Login extends StatefulWidget {
  Login({Key? key, arguments}) : super(key: key);

  @override
  _LoginState createState() => _LoginState();
}

class _LoginState extends State<Login> {
  // var flag = false;
  // FocusNode phoneFocusNode = FocusNode();
  // FocusNode yzmFocusNode = FocusNode();
  String phone = '15889476453';
  String verification = '9527';
  String sessionId = '';
  bool flag = true;

  // 手机号改变
  void onChangedPhone(val) {
    print(val);
    setState(() {
      phone = val;
    });
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        child: Column(
          children: [
            LoginHeader(name: "register"),
            SizedBox(
              height: 30,
            ),
            Padding(
              padding: EdgeInsets.all(20),
              child: Column(
                children: [
                  InputAdds(
                      phones: phone,
                      callBack: (value) => onChangedPhone(value)),
                  SizedBox(
                    height: 10,
                  ),
                  // Divider(color:Colors.white ,),
                  InputYzm(
                    yzm: verification,
                    phone: phone,
                    callBack: (val) {
                      setState(() {
                        verification = val;
                      });
                    },
                    callBackId: (val) {
                      print(val);
                      setState(() {
                        sessionId = val;
                      });
                    },
                  ),

                  Protocol(callBack: (val) {
                    print(val);
                    setState(() {
                      flag = val;
                    });
                  }),
                  SizedBox(
                    height: 40,
                  ),
                  Container(
                    width: double.infinity,
                    height: 50,
                    child: OnButton(
                        name: "登录",
                        callBack: () {
                          if (verification == '' || phone == '') {
                            EasyLoading.showToast("输入不能为空");
                            return;
                          }
                          if (verification.length != 4) {
                            EasyLoading.showToast("验证码错误");
                            return;
                          }
                          if (!flag) {
                            EasyLoading.showToast("请阅读用户协议");
                            return;
                          }
                          if (!Yz().isPhone(phone)) {
                            return;
                          }
                          EasyLoading.show(status: 'loading...');
                          HttpUtil().postHttp("/tbr/login", parameters: {
                            'phone': this.phone,
                            'sessionId': this.sessionId,
                            'code': this.verification,
                            'deviceId': '8'
                          }, onSuccess: (Map res) async {
                            print(res);
                            if (res['code'] == '200') {
                              EasyLoading.dismiss();
                              String data = jsonEncode(res);
                              // print(data);
                              SharedPreferences prefs =
                                  await SharedPreferences.getInstance();
                              await prefs.setString('loginInfo', data);
                              Navigator.pushNamed(context, '/');
                            }
                          });
                        }),
                  ),
                  SizedBox(
                    height: 40,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
